package ar.pmt.vista.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.StringTokenizer;

/**
 * @author DMR Consulting
 */
public class ExceptionInfo {

   /**
    * Classname String
    */
   private String className;

   /**
    * method String
    */
   private String method;

   /**
    * line String
    */
   private String line;

   /**
    * stack String
    */
   private String stack;

   /**
    *
    * @return devuelve el ClassName
    */
   public String getClassName() {
      return className;
   }

   /**
    *
    * @param className String
    */
   public void setClassName(String className) {
      this.className = className;
   }

   /**
    *
    * @return devuelve Line
    */
   public String getLine() {
      return line;
   }

   /**
    *
    * @param line String
    */
   public void setLine(String line) {
      this.line = line;
   }

   /**
    *
    * @return devuelve Method
    */
   public String getMethod() {
      return method;
   }

   /**
    *
    * @param method String
    */
   public void setMethod(String method) {
      this.method = method;
   }

   /**
    *
    * @param level int
    * @param t Throwable
    */
   protected void create(int level, Throwable t) {

      StringWriter sw = new StringWriter();
      t.printStackTrace(new PrintWriter(sw));

      String stacktrace = sw.toString();

      StringTokenizer st = new StringTokenizer(stacktrace, "\n");
      st.nextToken();
      for (int i = 0; i < level + 1 && st.hasMoreElements(); i++) {
         st.nextToken();
      }
      if (st.hasMoreElements()) {
         String token = st.nextToken();

         //Controlamos que al buscar los patterns no tengamos un IndexOutOfBounds sore el token
         if ((token.indexOf("at ")!=-1) && (token.lastIndexOf("(")>token.indexOf("at "))){
            String all =
               token.substring(
                  token.indexOf("at ") + 3,
                  token.lastIndexOf("("));

            //Controlamos que al buscar los patterns no tengamos un IndexOutOfBounds sore all
            if (all.lastIndexOf(".")!=-1){
               className = all.substring(0, all.lastIndexOf("."));

               method = all.substring(all.lastIndexOf(".") + 1);
            }
            //Controlamos que al buscar los patterns no tengamos un IndexOutOfBounds sore token
            if ((all.lastIndexOf(":")!=-1) && (token.lastIndexOf(")")>token.indexOf(":"))){
               line =
                  token.substring(
                     token.lastIndexOf(":") + 1,
                     token.lastIndexOf(")"));
            }
         }
         StringBuffer stb = new StringBuffer();
         stb.append(token + "\n");
         while (st.hasMoreElements()) {
            stb.append(st.nextToken() + "\n");
         }
         stack = stb.toString();
      }
   }

   /**
    *
    * @param level int
    */
   public ExceptionInfo(int level) {
      create(level, new Throwable());
   }

   /**
    * SourceFileInfo
    *
    */
   public ExceptionInfo() {
      create(0, new Throwable());
   }

   /**
    *
    * @param level int
    * @param t Throwable
    */
   public ExceptionInfo(int level, Throwable t) {
      create(level + 1, t);
   }

   /**
    *
    * @param t Throwable
    */
   public ExceptionInfo(Throwable t) {
      create(-1, t);
   }

   /**
    *
    * @return Stack
    */
   public String getStack() {
      return stack;
   }

   /**
    *
    * @param stack String
    */
   public void setStack(String stack) {
      this.stack = stack;
   }
}